<script setup lang="tsx">
import { PropType } from 'vue'
import { CurdData, FieldEnum, SchemaConfig } from '@/api/customer/types'
import { Descriptions } from '@/components/Descriptions'
import useSchema from './hooks/useSchema'
import { getFieldEnumApi } from '@/api/customer'
import { useCrudSchemas, AllSchemas } from '@/hooks/web/useCrudSchemas'
import { getSelectListApi as getOperatorSelectListApi } from '@/api/operator'
import { SelectOption } from '@/api/global/types'

defineProps({
  currentRow: {
    type: Object as PropType<Nullable<CurdData>>,
    default: () => null
  }
})

const { query } = useRoute()

let customerFieldEnum: FieldEnum = {}
const getCustomerFieldEnumList = async () => {
  const res = await getFieldEnumApi({ field: 'all' })
  customerFieldEnum = res.data
}

let operatorOptions: SelectOption[] = []
const getOperatorSelectList = async () => {
  const res = await getOperatorSelectListApi({})
  operatorOptions = res.data.list
}

const allSchemas = ref<AllSchemas>({
  formSchema: [] // 确保与接口匹配
})

const fetchData = async () => {
  await getCustomerFieldEnumList()
  await getOperatorSelectList()

  const schemaConfig: any = {
    fieldEnum: customerFieldEnum,
    operatorOptions,
    fromPage: query.source === '/public-page' ? 'public' : ''
  }
  const { crudSchemas } = useSchema(schemaConfig)
  const { allSchemas: newSchemas } = useCrudSchemas(crudSchemas)

  // 合并新的数据，确保不改变原有数据的类型
  allSchemas.value = {
    detailSchema: newSchemas.detailSchema || []
  }
}

fetchData()
</script>

<template>
  <Descriptions :schema="allSchemas.detailSchema" :data="currentRow || {}" />
</template>
